/**
 * @(#) ProfileProxy.as
 *
 * @date		2009. 02. 12.
 * @version		1.0
 * @author		Shin, Yunwoo
 * @description	 인사기록 조회 - Proxy
 * 				인사기록을 조회하는 Model Logic 
 */
package com.skt.common_hr.yearadjustment.model
{
	import com.skt.common_hr.common.Variables;
	import com.skt.common_hr.common.puremvc.CommonProxy;
	import com.skt.common_hr.yearadjustment.YearAdjustmentFacade;
	import com.skt.common_hr.yearadjustment.model.vo.YearAdjustmentVO;
	import com.wcl.dataloader.*;
	import com.wcl.utils.StringUtil;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.managers.CursorManager;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;

	public class YearAdjustmentProxy extends CommonProxy
	{
		public static const NAME:String = "YearAdjustmentProxy";
		
		public var mainList:ArrayCollection = new ArrayCollection();


		/**
		 * 생성자
		 * 
		 * @param
		 * @return void
		 */
		public function YearAdjustmentProxy(data:Object=null)
		{
			super(NAME, data);
		}
		
		
		/**
		 * data load를 수행
		 * 		- DataLoader 를 생성하고 Import Parameter 객체를 생성, RFC를 호출
		 * 
		 * @param pernr 사번
		 * @return void
		 */
		public function load(pernr:String):void
		{
			//var loader:IDataLoader = DataLoaderFactory.getDataLoader("Z_HR_0081");
			
			var param:Object = new Object();
			param["I_PERNR"] = pernr;
			param["I_YEA"] = Variables.I_YEA;
			loadData("Z_HR_0081_NEW", param, result, fault); 
			
		}
		
		public function save(pernr:String, year:String, detail:Array):void{
			//var loader:IDataLoader = DataLoaderFactory.getDataLoader("Z_HR_0088");
			
			var param:Object = new Object();
			param["I_PERNR"] = pernr;
			param["I_YEAR"] = year;
			var table:Object = new Object();
			table["ITAB"] = detail;
			saveData("Z_HR_0088_NEW", param, table, saveResult, saveFault); 
			
		} 
		
		
		
		
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event Listeners
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 * 
		 * @param
		 * @return void
		 */
		public function result(event:ResultEvent):void
		{
			if(retCode==0) {   
				trace( "suss to load YearAdjustment");
				mainList = parseMainList(event.result.ITAB);
				facade.sendNotification(YearAdjustmentFacade.LOAD_MAINLIST_COMPLETE, this);
			}
			else {
				trace( "failed to load YearAdjustment: (" +event.result["RETCODE"] + ") "+event.result["RETTEXT"]);
				facade.sendNotification(YearAdjustmentFacade.LOAD_MAINLIST_FAILED, this);
			}
		}
		
		/**
		 * 서버에서 오류가 발생했을 처리
		 * 
		 * @param
		 * @return void
		 */
		private function fault(event:FaultEvent):void
		{
			trace("Fault message in YearAdjustmentProxy: " + event.fault.faultString);
			facade.sendNotification(YearAdjustmentFacade.LOAD_MAINLIST_FAILED, this);
			CursorManager.removeBusyCursor();
		}
		
		public function saveResult(event:ResultEvent):void
		{
			if(event.result["RETCODE"]==0) {  
				trace( "suss to save YearAdjustment");
				facade.sendNotification(YearAdjustmentFacade.SAVE_MAINDATA_COMPLETE, this);
			}
			else {
				Alert.show("(" +event.result["RETCODE"] + ") " + event.result["RETTEXT"] );
				trace( "failed to save YearAdjustment: (" +event.result["RETCODE"] + ") "+event.result["RETTEXT"]);
				facade.sendNotification(YearAdjustmentFacade.SAVE_MAINDATA_FAILED, this);
			}
			CursorManager.removeBusyCursor();
		}
		
		/**
		 * 서버에서 오류가 발생했을 처리
		 * 
		 * @param
		 * @return void
		 */
		private function saveFault(event:FaultEvent):void
		{
			trace("Fault save message in YearAdjustmentProxy: " + event.fault.faultString);
			facade.sendNotification(YearAdjustmentFacade.SAVE_MAINDATA_FAILED, this);
			CursorManager.removeBusyCursor();
		}
		
		
		
		
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Data Parsing functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 전달된 result 객체를 분석하여 Value Object를 생성
		 * 
		 * @param
		 * @return 연말정산  MainList Value Object
		 */
		private function parseMainList(arr:ArrayCollection):ArrayCollection
		{
			var returnArr:ArrayCollection = new ArrayCollection();
			//Alert.show("parseMainList: " + arr.length);
			for each(var reg:Object in arr){
				var vo:YearAdjustmentVO = new YearAdjustmentVO();
				vo.YEAR		= reg["YEAR"];
				vo.PERNR	= StringUtil.trimFront(reg["PERNR"], "0");
				vo.ENAME	= reg["ENAME"];
				vo.ORGTX	= reg["ORGTX"];
				vo.TAXPAY	= reg["TAXPAY"];
				vo.PRETAX	= reg["PRETAX"];
				vo.FIXTAX	= reg["FIXTAX"];
				vo.MINTAX	= reg["MINTAX"];
				//vo.EDTPR	= reg["EDTPR"];
				vo.CGBN = reg["CGBN"];
				vo.DTEXT	= reg["DTEXT"];
				
				//2014
				vo.PRELO		= reg["PRELO"];								
				returnArr.addItem(vo);
			}
			return returnArr;
		}
		
	}
}